import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
import * as dat from 'dat.gui';

class CameraController {
    constructor(camera, domElement) {
        this.camera = camera;
        // 创建 OrbitControls
        this.controls = new OrbitControls(camera, domElement);
        this.controls.enableDamping = true; // 添加阻尼效果
        this.controls.dampingFactor = 0.05;
        
        // 设置控制限制
        this.controls.minDistance = 5;
        this.controls.maxDistance = 30;
        this.controls.maxPolarAngle = Math.PI / 2; // 限制相机垂直角度
        this.controls.enablePan = false; // 禁用相机平移
        
        // 创建调试界面
        this.gui = new dat.GUI();
        this.setupDebugUI();
    }

    setupDebugUI() {
        const cameraFolder = this.gui.addFolder('Camera');
        // 相机位置控制
        const position = this.camera.position;
        cameraFolder.add(position, 'x', -50, 50).name('Camera X');
        cameraFolder.add(position, 'y', 0, 50).name('Camera Y');
        cameraFolder.add(position, 'z', -50, 50).name('Camera Z');
        
        // 控制器参数
        cameraFolder.add(this.controls, 'enabled').name('Controls Enabled');
        cameraFolder.add(this.controls, 'dampingFactor', 0, 0.1).name('Damping');
        cameraFolder.add(this.controls, 'minDistance', 1, 10).name('Min Distance');
        cameraFolder.add(this.controls, 'maxDistance', 10, 100).name('Max Distance');
        cameraFolder.open();
    }

    tick() {
        this.controls.update();
    }

    dispose() {
        this.controls.dispose();
        this.gui.destroy();
    }
}

export { CameraController }; 